<?php

##################################################
#
# Copyright (c) 2004-2006 OIC Group, Inc.
# Written and Designed by James Hunt
#
# This file is part of Exponent
#
# Exponent is free software; you can redistribute
# it and/or modify it under the terms of the GNU
# General Public License as published by the Free
# Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# GPL: http://www.gnu.org/licenses/gpl.txt
#
##################################################

class file {
	// 4/1/2010: Immanuel192 add this code to detect mime type by file extensition
	function mimetype_detect($object, $filename){
		$mimetype_document =  array("doc" => "application/msword", "oda" => "application/oda", "pdf" => "application/pdf", "xhtml" => "application/xhtml+xml",
         "xht" => "application/xhtml+xml", "css" => "text/css", "html" => "text/html", "htm" => "text/html", "asc" => "text/plain", "txt" => "text/plain",
         "rtx" => "text/richtext", "rtf" => "text/rtf", "xml" => "text/xml", "xsl" => "text/xml");
		$mimetype_image =   array("bmp" => "image/bmp","gif" => "image/gif","ief" => "image/ief","jpeg" => "image/jpeg","jpg" => "image/jpeg","jpe" => "image/jpeg",
         "png" => "image/png","tiff" => "image/tiff","tif" => "image/tif","djvu" => "image/vnd.djvu","djv" => "image/vnd.djvu","wbmp" => "image/vnd.wap.wbmp","ras" => "image/x-cmu-raster","pnm" => "image/x-portable-anymap",
         "pbm" => "image/x-portable-bitmap","pgm" => "image/x-portable-graymap","ppm" => "image/x-portable-pixmap","rgb" => "image/x-rgb","xbm" => "image/x-xbitmap","xpm" => "image/x-xpixmap","xwd" => "image/x-windowdump"
		);
		$mimetype_video =  array ("flv" => "video/flv" );
		$mimetype_audio =  array ("mp3" => "audio/mp3"  );
		
		$temp_file_name=basename($filename);
		$temp_file_name = explode('.', $temp_file_name);
		// take the last part of the file to get the file extension
		$temp_file_name = strtolower($temp_file_name[count($temp_file_name)-1]);  
		if (isset($mimetype_audio[$temp_file_name]))
			$object->is_audio=1;
		if (isset($mimetype_video[$temp_file_name]))
			$object->is_video=1;
		if (isset($mimetype_image[$temp_file_name]))
			$object->is_image=1;
		return $object;
	}
	
	function update($name,$dest,$object,$destname = null,$force=false) {
		$i18n = exponent_lang_loadFile('datatypes/file.php');
		
		if (!defined('SYS_FILES')) include_once(BASE.'subsystems/files.php');
		
		// Get the filename, if it was passed in the update() call.  Otherwise, fallback
		if ($destname == null) {
			$object->filename = time()."_".$_FILES[$name]['name'];
		} else {
			$object->filename = $destname;
		}
		// General error message.  This will be made more explicit later on.
		$err = sprintf($i18n['cant_upload'],$object->filename) .'<br />';
		
		switch($_FILES[$name]['error']) {
			case UPLOAD_ERR_OK:
				// Everything looks good.  Continue with the update.
				break;
			case UPLOAD_ERR_INI_SIZE:
			case UPLOAD_ERR_FORM_SIZE:
				// This is a tricky one to catch.  If the file is too large for POST, then the script won't even run.
				// But if its between post_max_size and upload_file_max_size, we will get here.
				return $err.$i18n['file_too_large'];
			case UPLOAD_ERR_PARTIAL:
				return $err.$i18n['partial_file'];
			case UPLOAD_ERR_NO_FILE:
				return $err.$i18n['no_file_uploaded'];
			default:
				return $err.$i18n['unknown'];
				break;
		}
		
		// Fix the filename, so that we don't have funky characters screwing with out attempt to create the destination file.
		$object->filename = exponent_files_fixName($object->filename);
	
			
		if (file_exists(BASE.$dest.'/'.$object->filename) && $force == false) {
			return $err.$i18n['file_exists'];
		}
	
		//Check to see if the directory exists.  If not, create the directory structure.
		if (!file_exists(BASE.$dest)) {
			exponent_files_makeDirectory($dest);
		}	

		// Move the temporary uploaded file into the destination directory, and change the name.
		exponent_files_moveUploadedFile($_FILES[$name]['tmp_name'],BASE.$dest.'/'.$object->filename);
		if (!file_exists(BASE.$dest.'/'.$object->filename)) {
			return $err.$i18n['cant_move'];
		}
		
		// At this point, we are good to go.
		
		$object->mimetype = $_FILES[$name]['type'];
		$object->directory = $dest;
		//$object->accesscount = 0;
		$object->filesize = $_FILES[$name]['size'];
		$object->posted = time();
		global $user;
		if ($user) {
			$object->poster = $user->id;
		}
		$object->last_accessed = time();
		$object->is_image = 0; $object->is_video = 0; $object->is_audio = 0;
		
		// 3/29/2010: Immanuel192 change this code to support file type detection
		// get file extention
		$object=file::mimetype_detect($object,$_FILES[$name]['name']);
		// end 3/29/2010 change
		return $object;
	}
	
	function delete($file) {
		if ($file == null) {
			return true;
		}
		
		if (is_readable(BASE.$file->directory) && !file_exists(BASE.$file->directory.'/'.$file->filename)) {
			return true;
		}
		
		if (is_really_writable(BASE.$file->directory)) {
			unlink($file->directory.'/'.$file->filename);
			if (!file_exists(BASE.$file->directory.'/'.$file->filename)) {
				return true;
			}
		}
		return false;
	}
	
	function findByType($item_type) {
                global $db;
                return $db->selectObjects('file', 'id IN (SELECT file_id FROM '.DB_TABLE_PREFIX.'_file_details WHERE item_type="'.$item_type.'")');
        }

        function findFilesForItem($item_type, $item_id) {
                global $db;
                return $db->selectObjects('file', 'id IN (SELECT file_id FROM '.DB_TABLE_PREFIX.'_file_details WHERE item_type="'.$item_type.'" AND item_id='.$item_id.')');
        }
}

?>
